namespace Trader.DataServices.Yahoo
{
    using System;
    using System.Linq;
    using BO;
    using BO.Interface;

    partial class Quote : IQuote
    {
    }

    partial class Currency : ICurrecny
    {
        #region ICurrecny Members

        public decimal ConvertTo(ICurrecny targetCurrecny, DateTime date)
        {
            ExchangeRate exchangeRate =
                ExchangeRates.FirstOrDefault(rate => rate.Date == date && rate.Currency1 == targetCurrecny);
            if (exchangeRate != null)
            {
                return exchangeRate.Rate;
            }
            exchangeRate = ExchangeRates1.FirstOrDefault(rate => rate.Date == date && rate.Currency == targetCurrecny);
            if (exchangeRate != null)
            {
                return 1/exchangeRate.Rate;
            }

            DateTime lastDate;
            if (ExchangeRates.Where(rate => rate.Date < date && (rate.Currency1 == targetCurrecny)).Any())
            {
                lastDate = ExchangeRates
                    .Where(rate => rate.Date < date && (rate.Currency1 == targetCurrecny)).Max(rate => rate.Date);
            }
            else
            {
                lastDate = ExchangeRates1
                    .Where(rate => rate.Date < date && (rate.Currency == targetCurrecny)).Max(rate => rate.Date);
            }

            return ConvertTo(targetCurrecny, lastDate);
        }

        #endregion

        public override string ToString()
        {
            return Name;
        }
    }

    partial class Stock : IStock
    {
        private IQuotesCollection stockQuotes;

        #region IStock Members

        IQuotesCollection IStock.Quotes
        {
            get
            {
                if (stockQuotes == null)
                {
                    InitializeQuotesCollection();
                }

                return stockQuotes;
            }
        }

        public IMoney AsMoney(int units, DateTime date)
        {
            return new Money(units*stockQuotes[date].Closing, StockExchange.Currency);
        }

        public int UpdateUnits(int units, DateTime date)
        {
            Split dateSplit = Splits.Where(split => split.Date == date).FirstOrDefault();
            if (dateSplit == null)
            {
                return units;
            }

            return (int) Math.Floor(dateSplit.Ratio*units);
        }

        public int? GetSearchResults(DateTime dateTime)
        {
            var minDate = dateTime - TimeSpan.FromDays(3);
            var searchResult = SearchResults.FirstOrDefault(result => result.Date <= dateTime && result.Date >= minDate);
            if(searchResult==null)
            {
                return null;
            }

            return searchResult.Results;
        }

        public int CalulateUnitsInMoney(IMoney money, DateTime date)
        {
            decimal value = money.In(StockExchange.Currency, date);
            return (int) Math.Floor(value/stockQuotes[date].Closing);
        }

        #endregion

        public void CachData(DateTime date, int signalID, int recommendation)
        {
            SignalsInstances.Add(new SignalsInstance {Date = date, SignalID = signalID, Recommendation = recommendation});
        }

        private void InitializeQuotesCollection()
        {
            stockQuotes = new QuotesCollection();
            foreach (Quote quote in Quotes)
            {
                stockQuotes.Add(quote.Date, quote);
            }
        }

        public override string ToString()
        {
            return string.Format("{0} ({1})", Name, StockExchange.Name);
        }
    }
}

//"c:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin\SqlMetal.exe" "d:\Documents\Visual Studio 2008\Projects\AdvancedProgramming\Trader\Trader\Trader.DataServices.Yahoo\HistoricalData.sdf" "/dbml:d:\Documents\Visual Studio 2008\Projects\AdvancedProgramming\Trader\Trader\Trader.DataServices.Yahoo\HistoricalDataClasses.dbml" "/namespace:Trader.DataServices.Yahoo" "/pluralize"